package c926.xiaoduyun.xiaodu.mservice;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.form.container.Wizard;
import kd.bos.form.control.Button;
import kd.bos.form.control.Steps;
import kd.bos.form.control.StepsOption;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.control.events.StepEvent;
import kd.bos.form.control.events.WizardStepsListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class XiaoduStepWizardPlugin extends AbstractFormPlugin implements WizardStepsListener {

    public void initialize() {//初始化监听控件
        addClickListeners("c926_buttonap");
        Wizard wizard = this.getControl("c926_wizardap");
        wizard.addWizardStepsListener(this);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        Wizard wizard = this.getControl("c926_wizardap");
        List<StepsOption> stepsOptions = wizard.getStepsOptions();
        stepsOptions.clear();
        DynamicObjectCollection recordStep = this.getModel().getEntryEntity("c926_record_step");

        int i = 0;
        //设置当前进行到哪步的变量---当前步数,
        int currentindex = -1;
        //每次进入的时候重置一下页面缓存的当前步数,currentstep这个会在update方法中使用的
        if (this.getPageCache().get("currentstep") != null) {
            this.getPageCache().remove("currentstep");
        }
        for (DynamicObject entryData : recordStep) {
            StepsOption stepsOption0 = new StepsOption();
            String level = entryData.getString("c926_level.name");
            String step = entryData.getString("c926_step.name");
            stepsOption0.setTitle(new LocaleString(level));
            stepsOption0.setDescription(new LocaleString(step));
            //根据不同的单据体内的状态设定不一样步骤状态
            if("C".equals(entryData.get("c926_status"))) {
                stepsOption0.setStatus(Steps.FINISH);
                i++;
            }
            else if("A".equals(entryData.get("c926_status"))){
                stepsOption0.setStatus(Steps.PROCESS);
                i++;
            }else if("B".equals(entryData.get("c926_status"))) {
                currentindex=i;
                this.getPageCache().put("currentstep",String.valueOf(currentindex));
                i++;
            }
            stepsOptions.add(stepsOption0);

        }

        // 更新步骤条设置
        wizard.setWizardStepsOptions(stepsOptions);

        // 设置当前节点
        Map<String, Object> currentStepMap = new HashMap<>();
        if (currentindex >= 0) {
            currentStepMap.put("currentStep", currentindex);
            currentStepMap.put("currentStatus", Steps.PROCESS);
            this.getModel().setValue("c926_entitypicture", this.getModel().getValue("c926_picture", i - 1));
        } else {
            currentStepMap.put("currentStep", i - 1);
            currentStepMap.put("currentStatus", Steps.FINISH);
            this.getView().setVisible(false, "c926_buttonap");
        }
        // 更新当前节点
        wizard.setWizardCurrentStep(currentStepMap);
        System.out.println();
    }


    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        Button button = (Button) evt.getSource();
        if (button.getKey().equals("c926_buttonap")) {
            if (this.getModel().getValue("c926_picture") == null || "".equals(this.getModel().getValue("c926_picture"))) {
                this.getView().showErrorNotification("必须上传消毒图片！");
                evt.setCancel(true);
            } else {
                DynamicObjectCollection stepCollection = this.getModel().getEntryEntity("c926_record_step");
                // 是否是最后一步的变量
                boolean ifEndFlag = true;
                for (DynamicObject entrydata : stepCollection) {
                    // 进行中的步骤改成已完成
                    if ("B".equals(entrydata.get("c926_status"))) {
                        entrydata.set("c926_status", "C");
                        entrydata.set("c926_entitypicture", this.getModel().getValue("c926_picture"));
                    }
                    //未进行的改成进行中
                    if ("A".equals(entrydata.get("c926_status"))) {
                        entrydata.set("c926_status", "B");
                        //如果有未进行的改成进行中,则说明不是最后一步
                        ifEndFlag = false;
                        //并且把单据状态改成对应的状态
                        this.getModel().setValue("billstatus", "A");
                        //改完下一条就退出循环
                        break;
                    }
                }
                if (ifEndFlag) {
                    //所有步骤全部完成，则完成
                    this.getModel().setValue("billstatus", "B");
                    String billno = (String) this.getModel().getValue("billno");
                    QFilter filter = new QFilter("c926_xiaoduid", QCP.equals, billno);
                    DynamicObject xiaoduApply = BusinessDataServiceHelper.loadSingle("c926_xiaoduapply","billstatus", filter.toArray());
                    xiaoduApply.set("billstatus","F");
                    SaveServiceHelper.update(xiaoduApply);
                }
                this.getView().updateView();
            }
        }
    }


    @Override
    public void update(StepEvent stepEvent) {
        int stepint = stepEvent.getValue();
        this.getView().setVisible(true, "c926_buttonap");
        String currentstep = this.getPageCache().get("currentstep");
        if (currentstep != null) {
            this.getView().setVisible(currentstep.equals(String.valueOf(stepint)), "c926_buttonap");
        } else {
            this.getView().setVisible(false, "c926_buttonap");
        }
        this.getModel().setValue("c926_entitypicture", this.getModel().getValue("c926_picture", stepint));

    }
}